package com.example.jk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.example.jk.common.Result;
import com.example.jk.entity.User;
import com.example.jk.mapper.UserMapper;
import com.example.jk.service.UserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cjx
 * @since 2021-04-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Result add(User user) {
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("phone",user.getPhone());
        if(user.selectOne(wrapper)!=null){
            return Result.error("该手机号已注册");
        }
        if(!user.insert()){
            return Result.error();
        }
        return Result.success();
    }

    @Override
    public Result update(User user) {
        if(!user.updateById()){
            return Result.error();
        }
        return Result.success();
    }

    @Override
    public Result log(User user) {
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("phone",user.getPhone());

        if (user.selectOne(wrapper) == null){
            return Result.error("账号不存在");
        }
        wrapper.eq("pass",user.getPass());
        if (user.selectOne(wrapper) == null){
            return Result.error("密码错误");
        }
        return Result.success("登录成功",user.selectOne(wrapper));
    }

    @Override
    public Result dateil(User user) {
        return Result.success(user.selectById());
    }

    @Override
    public Result retrievePassword(User user) {
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("phone",user.getPhone());
        if(user.selectOne(wrapper) == null){
            return Result.error("手机号不存在");
        }
        wrapper.eq("name",user.getName());
        if(user.selectOne(wrapper) == null){
            return Result.error("姓名不存在");
        }
        wrapper.eq("id_car",user.getId_car());
        if(user.selectOne(wrapper) == null){
            return Result.error("身份证号不存在");
        }
        return Result.success(user.selectOne(wrapper));
    }

}
